﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMSpaceShooter.Screens;
using Microsoft.Xna.Framework;
using FMSpaceShooter.GameObjects.ItemFolder;

namespace FMSpaceShooter.GameObjects.EnemyFolder
{
    class Boss1:EnemyBase
    {

        public new const string TypeName = "Boss1";
        public const string imageName = "Images/Enemies/LevelONEboss";
        private double time;
        private Vector2 direction;
        private int randomInt = 1;
        public override bool isBoss { get { return true; } }
        public override int Points { get { return 200; } }


         public Boss1(GameplayScreen screen, Vector2 position)
            :base(screen, imageName, position, 3000)
        {
            time = 0;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (screen.GOM.PlayerOne.IsInPlay)
            {


                time -= gameTime.ElapsedGameTime.Milliseconds;
                if (time<gameTime.ElapsedGameTime.Milliseconds)
                {
                    switch (randomInt)
                    {
                        case 1:
                            direction = screen.GOM.PlayerOne.Position - position;
                            break;
                        case 2:
                            direction = new Vector2(screen.Random.Next(200), screen.Random.Next(200));
                            break;
                    }

                    randomInt = screen.Random.Next(2) + 1;
                    time = gameTime.ElapsedGameTime.Milliseconds+2000;
                }




                direction.Normalize();
                position += (direction * (float)(gameTime.ElapsedGameTime.TotalSeconds * 100))*2;
                rotation = Util.Tools.VectorToAngle(direction);

                float spawnRotation = rotation;
                if (screen.Random.Next(100) < 3  &&  screen.GOM.Negotiator==null)
                {
                    screen.GOM.Level.enemyCount += 1;
                    int x = screen.Random.Next(5);
                    switch (x)
                    {
                        case 1:
                            spawnRotation += (float)Math.PI / 4;
                            break;
                        case 2:
                            spawnRotation += (float)Math.PI / 2;
                            break;
                        case 3:
                            spawnRotation += (float)Math.PI * 3 / 2;
                            break;
                        default:
                            break;
                    }
                    screen.GOM.AddToPlay(new EnemyShell(screen, position, spawnRotation, this));
                }

            }
            else
            {
                rotation += (float)(1f * gameTime.ElapsedGameTime.TotalSeconds);
            }

            checkBoundries();


        }

        public override void Die()
        {
            base.Die();
            int x = screen.Random.Next(20);

            if (x < 6)
            {
                screen.GOM.AddToPlay(new SplitterBulletBoost(screen, this.position));
            }
            else if (x > 15)
            {
                screen.GOM.AddToPlay(new ExplosionBulletBoost(screen, this.position));
            }
            else 
            {
                screen.GOM.AddToPlay(new PrimaryBulletBoost(screen, this.position));
            }

        }



        private void checkBoundries()
        {
            Vector2 res = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
             screen.GraphicsDeviceManager.PreferredBackBufferHeight);
            if (position.X < 0)
            {
                position.X = 0 ;
                direction.X *= -1f;
            }
            else if (position.X > res.X)
            {
                position.X = res.X;
                direction.X *= -1f;
            }
            if (position.Y < 0)
            {
                position.Y = 0;
                direction.Y *= -1f;
            }
            else if (position.Y > res.Y)
            {
                position.Y = res.Y;
                direction.Y *= -1f;
            }

        }

      

    }
}
